package com.cat.model.web.action.admin;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.page.Page;
import com.alonew.json.JSONArray;
import com.cat.model.biz.logic.BasLogic;
import com.cat.model.biz.service.admin.GraphicsInfoService;
import com.cat.model.common.model.common.BasConfigVO;
import com.cat.model.common.model.common.GraphicsInfoVO;
import com.cat.model.common.model.common.GraphicsStyleVO;
import com.cat.model.common.model.common.PlmCityVO;
import com.cat.model.common.model.common.PlmProvinceVO;
import com.cat.model.common.util.Constant;
import com.cat.model.common.util.ImageUtil;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.web.action.base.AdminAction;

public class GraphicsInfoManagerAction extends AdminAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 425636316597986569L;

	private GraphicsInfoVO graphicsInfo;
	private Long id;
	private File image;
	private String imageFileName; 
	private String imageUrl;
	private String 	cityListStr;
	@Autowired
	private GraphicsInfoService graphicsInfoService; 
	
	@Autowired
	private PlmCommDAO plmCommDAO;
	
	@Autowired
	private ModelMapper modelMapper;
	
	@Autowired
	private BasLogic basLogic; 
	
	/**
	 * list
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		Map<String, Object> condition = new HashMap<String, Object>();
		
		Page page = this.fetchPageParams();
		page.setNumPerPage(20);
		
		Long totalCount = plmCommDAO.searchSize("manager.searchGraphicsInfoSize", condition);
		page.setTotalCount(totalCount);
		int start = (page.getPageNum() -1) * page.getNumPerPage();
		if(start < 0){
			start = 0;
		}
		
		List<GraphicsInfoVO> list = (List<GraphicsInfoVO>)plmCommDAO.search("manager.searchGraphicsInfo", condition, start, page.getNumPerPage());
		
		request.setAttribute("list", list);
		request.setAttribute("size", totalCount);
		return "list";
	}
	
	public String deleteSingle() {
		graphicsInfoService.deleteGraphicsInfo(id);
		okAlert("删除成功！");
		return "list";
	}
	
	public String toAdd(){
		List<BasConfigVO> styleList = basLogic.getBasConfigByName(Constant.CONFIG_NAME_GRAPHICS_STYLE_CODE);
		request.setAttribute("styleList", styleList);
		List<PlmCityVO> cityList = plmCommDAO.getAllPlmCity();
		List<PlmProvinceVO> plmProvinceList=plmCommDAO.getPlmProvince("CN");
		request.setAttribute("plmProvinceList", plmProvinceList);
		request.setAttribute("cityList", cityList);
		//cityListStr=(new JSONArray(cityList)).toString();
		//System.out.println(cityListStr);
		return "add";
	}
	
	public String add(){
		try {
			if(image != null){
				BufferedImage img = ImageIO.read(new FileInputStream(image));
				if(img == null){
					this.printHTML("<script>parent.callback(" + false + ", '"+"图片格式不正确"+"')</script>");
					return "list";
				}
			}
			String[] style = request.getParameterValues("style"); 
			graphicsInfoService.createGraphicsInfo(graphicsInfo,style,image, imageFileName);
		} catch (Exception e) {
			e.printStackTrace();
			parentAlert(false, e.getMessage());
			return "list";
		}
		
		parentAlert(true, "保存成功");
		return "list";
	}
	
	public String toEdit(){
		graphicsInfo = modelMapper.getGraphicsInfo(id);
		List<BasConfigVO> styleList = basLogic.getBasConfigByName(Constant.CONFIG_NAME_GRAPHICS_STYLE_CODE);
		request.setAttribute("styleList", styleList);
		List<PlmProvinceVO> plmProvinceList=plmCommDAO.getPlmProvince("CN");
		request.setAttribute("plmProvinceList", plmProvinceList);
		List<PlmCityVO> cityList = plmCommDAO.getAllPlmCity();
		request.setAttribute("cityList", cityList);
		List<GraphicsStyleVO> styles = modelMapper.getGraphicsStyleList(id);
		Map<String, Object> styleMap = new HashMap<String, Object>();
		if(styles!=null&&styles.size()>0){
			for (int i = 0; i < styles.size(); i++) {   
				GraphicsStyleVO styleVO = (GraphicsStyleVO)styles.get(i);
				styleMap.put(styleVO.getStyleCode(), styleVO.getStyleCode().toString());
			}
		}
		request.setAttribute("styleMap",styleMap);
		
		if(graphicsInfo != null){
			imageUrl = ImageUtil.wrapImageUrl(graphicsInfo.getImage());
		}
		
		return "edit";
	}
	
	public String edit(){
		try {
			if(image != null){
				BufferedImage img = ImageIO.read(new FileInputStream(image));
				if(img == null){
					this.printHTML("<script>parent.callback(" + false + ", '"+"图片格式不正确"+"')</script>");
					return "list";
				}
			}
			String[] style = request.getParameterValues("style"); 
			if(graphicsInfo.getCityCode().contains(",")){
				String[] strs = graphicsInfo.getCityCode().split(",");
				graphicsInfo.setCityCode(strs[0]);
			}
			
			graphicsInfoService.updateGraphicsInfo(graphicsInfo,style,image, imageFileName);
		} catch (Exception e) {
			e.printStackTrace();
			parentAlert(false, e.getMessage());
			return "list";
		}
			
		parentAlert(true, "保存成功");
		return "list";
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public GraphicsInfoVO getGraphicsInfo() {
		return graphicsInfo;
	}

	public void setGraphicsInfo(GraphicsInfoVO graphicsInfo) {
		this.graphicsInfo = graphicsInfo;
	}

	public File getImage() {
		return image;
	}

	public String getCityListStr() {
		return cityListStr;
	}

	public void setCityListStr(String cityListStr) {
		this.cityListStr = cityListStr;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageUrl() {
		return imageUrl;
	}

	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}
}
